var parseUrl = require('parseurl');
var qs = require('qs');

module.exports=function(options){
    var opts = Object.assign({}, options)
    var queryparse = qs.parse;
    if (typeof options === 'function') {
        queryparse = options;
        opts = undefined;
    }
    return function query(req, res, next){
        //console.log('req.query',req.query)
        if (!req.query) {
          var val = parseUrl(req).query;
          req.query = queryparse(val, opts);
        }
        //console.log(req.query);
        next();
    };
}